package com.xdwanj.servlet;

import com.mysql.jdbc.StringUtils;
import com.xdwanj.pojo.Banner;
import com.xdwanj.pojo.Commodity;
import com.xdwanj.service.CommodityService;
import com.xdwanj.service.impl.CommodityServiceImpl;
import com.xdwanj.util.ConstantsRes;
import com.xdwanj.util.ConstantsStr;
import com.xdwanj.util.ConstantsVal;
import com.xdwanj.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {

    /**
     * 根请求地址
     */
    public static final String REQUEST_MAPPING = "shop";

    /**
     * 方法属性名
     */
    public static final String METHOD = "method";

    /**
     * 查询Banner集合
     */
    public static final String RECOMMEND = "recommend";

    /**
     * 查询商品列表
     */
    public static final String COMMODITY_LIST = "commodityList";

    /**
     * 商品类别id
     */
    public static final String CAID = "caid";

    /**
     * 商品ID
     */
    public static final String COID = "coid";
    /**
     * 商品数量
     */
    public static final String COUNT = "count";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入ShopServlet...");
        String method = req.getParameter(METHOD);

        switch (method) {
            case RECOMMEND:
            case COMMODITY_LIST:
                queryCommodityList(req, resp);
                break;
            default:
                break;
        }
    }


    private void queryCommodityList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        String queryName = req.getParameter(ConstantsStr.QUERY_NAME);
        String caidStr = req.getParameter(CAID);
        String currentPageNoStr = req.getParameter(ConstantsStr.CURRENT_PAGE_NO);

        int caid = 0;
        int currentPageNo = 1;
        int pageSize = ConstantsVal.PAGE_SIZE;

        if (StringUtils.isNullOrEmpty(queryName)) {
            queryName = "";
        }

        if (StringUtils.isStrictlyNumeric(caidStr)) {
            caid = Integer.parseInt(caidStr);
        }

        if (StringUtils.isStrictlyNumeric(currentPageNoStr) && Integer.parseInt(currentPageNoStr) >= 1) {
            currentPageNo = Integer.parseInt(currentPageNoStr);
        }

        CommodityService commodityService = new CommodityServiceImpl();

        PageBean<Banner> bannerList = commodityService.findBannerList(pageSize);
        PageBean<Commodity> pageBean = commodityService.findCommodityList(queryName, caid, currentPageNo, pageSize);

        req.setAttribute(ConstantsStr.COMMODITY_LIST, pageBean);
        req.setAttribute(ConstantsStr.BANNER_LIST, bannerList);
        req.setAttribute(CAID, caid);

        url = ConstantsRes.SHOP_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
